package com.smartsaas.ugclivii.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.smartsaas.ugclivii.dto.MacZipDto;
import com.smartsaas.ugclivii.dto.ResultDTO;
import com.smartsaas.ugclivii.ui.UiContext;
import com.smartsaas.ugclivii.utils.RequestUtils;
import org.apache.http.client.utils.URIBuilder;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.Set;

public class Api {
    public static ResultDTO<MacZipDto> applyVersion(String host, String api, String mac, String partNumber){
        try {
            URI uri = new URIBuilder(host+api).addParameter("mac", mac).addParameter("partNumber",partNumber).build();
            String s = RequestUtils.get(uri);
            return JSON.parseObject(s,new TypeReference<ResultDTO<MacZipDto>>(){});
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public static URI buildRequest(String apiPath, Map<String,String> params){
        try{
            String host = UiContext.getInstance().getConfig().getHost();
            URIBuilder builder = new URIBuilder(host + apiPath);
            if (params!=null){
                Set<String> key = params.keySet();
                for (String s : key){
                    builder.addParameter(s,params.get(s));
                }
            }
            return builder.build();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
